<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="客户名称" prop="custName">
        <el-input v-model="queryParams.custName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="省、市、区" prop="queryProvinceAndCityAndDistrict">
        <el-cascader placeholder="请选择省、市、区" v-model="queryParams.queryProvinceAndCityAndDistrict" :options="options"
          @keyup.enter.native="handleQuery" @lazy-load="selectLoadOptions"
          :props="{ lazy: true,checkStrictly:true,multiple: true, lazyLoad: selectLoadOptions,value:'regionId',label:'regionName' }"
          clearable></el-cascader>
      </el-form-item>
      <el-form-item label="客户联系人" prop="linkName">
        <el-input v-model="queryParams.linkName" placeholder="请输入客户联系人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户联系电话" prop="linkTel">
        <el-input v-model="queryParams.linkTel" placeholder="请输入客户联系电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品包名称" prop="productName">
        <el-select v-model="queryParams.productName" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sale_product" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户等级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sale_customer_level" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="来源渠道" prop="sourceChannel">
        <el-select v-model="queryParams.sourceChannel" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sale_source_channel" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户状态" prop="signState">
        <el-select v-model="queryParams.signState" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sale_sign_state" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="付款状态" prop="isAllPay">
        <el-select v-model="queryParams.isAllPay" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sale_pay_state" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="跟进人" prop="nextDoUserName">
        <el-input v-model="queryParams.nextDoUserName" placeholder="请输入跟进人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['saledata:customer:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['saledata:customer:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['saledata:customer:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['saledata:customer:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-descriptions class="margin-top" title="销售单详情" :column="2" :size="small" border>
            <el-descriptions-item>
              <template slot="label">
                省、市、区
              </template>
              {{scope.row.provinceAndCityAndDistrict}}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                详细地址
              </template>
              {{scope.row.address}}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                客户联系人
              </template>
              {{scope.row.linkName}}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                客户联系电话
              </template>
              {{scope.row.linkTel}}
            </el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="客户名称" align="center" prop="custName" />
      <el-table-column label="产品包名称" align="center" prop="productName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sale_product" :value="scope.row.productName" />
        </template>
      </el-table-column>
      <el-table-column label="客户等级" align="center" prop="level" />
      <el-table-column label="来源渠道" align="center" prop="sourceChannel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sale_source_channel" :value="scope.row.sourceChannel" />
        </template>
      </el-table-column>
      <el-table-column label="客户状态" align="center" prop="signState" />
      <el-table-column label="付款状态" align="center" prop="isAllPay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sale_pay_state" :value="scope.row.isAllPay" />
        </template>
      </el-table-column>
      <el-table-column label="跟进人" align="center" prop="nextDoUserName" />
      <el-table-column label="备注" align="center" prop="mark" />
      <el-table-column label="添加时间" align="center" prop="gmtCreate" width="180">
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="120" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['saledata:customer:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['saledata:customer:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改销售单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">

            <el-form-item label="客户名称" prop="custName">
              <el-input v-model="form.custName" placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="省、市、区" prop="provinceAndCityAndDistrict">
              <el-cascader placeholder="请选择省、市、区" v-model="form.provinceAndCityAndDistrict" :options="options"
                @lazy-load="selectLoadOptions"
                :props="{ lazy: true, lazyLoad: selectLoadOptions,value:'regionId',label:'regionName' }"
                clearable></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户联系人" prop="linkName">
              <el-input v-model="form.linkName" placeholder="请输入客户联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户联系电话" prop="linkTel">
              <el-input v-model="form.linkTel" placeholder="请输入客户联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品包名称" prop="productName">
              <el-select v-model="form.productName" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sale_product" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="客户等级" prop="level">
              <el-select v-model="form.level" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sale_customer_level" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来源渠道" prop="sourceChannel">
              <el-select v-model="form.sourceChannel" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sale_source_channel" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户状态" prop="signState">
              <el-select v-model="form.signState" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sale_sign_state" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款状态" prop="isAllPay">
              <el-select v-model="form.isAllPay" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.sale_pay_state" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="跟进人" prop="nextDoUserName">
              <el-input v-model="form.nextDoUserName" placeholder="请输入跟进人" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="mark">
              <el-input type="textarea" maxlength="2000" show-word-limit v-model="form.mark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="submitLoading">
          {{ submitLoading ? '提交中...' : '确定' }}</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCustomer,
    getCustomer,
    delCustomer,
    addCustomer,
    updateCustomer
  } from "@/api/bus_web/saledata/customer";
  import {
    loadOptions
  } from "@/api/system/sys_region";
  export default {
    name: "Customer",
    dicts: ['sale_pay_state', 'sale_sign_state', 'sale_customer_level', 'sale_source_channel', 'sale_product'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        submitLoading: false,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        options: [],
        // 总条数
        total: 0,
        // 销售单表格数据
        customerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          custName: null,
          province: null,
          city: null,
          district: null,
          address: null,
          linkName: null,
          linkTel: null,
          productName: null,
          productPrice: null,
          level: null,
          sourceChannel: null,
          signState: null,
          isAllPay: null,
          nextDoUserName: null,
          queryProvinceAndCityAndDistrict: null,
          mark: null,
          deptId: null,
          gmtCreate: null,
          gmtModify: null,
          deleted: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          linkName: [{
            required: true,
            message: "联系人不能为空",
            trigger: "blur"
          }],
          linkTel: [{
            required: true,
            message: "联系电话不能为空",
            trigger: "blur"
          }],
          custName: [{
            required: true,
            message: "客户姓名不能为空",
            trigger: "blur"
          }],
          signState: [{
            required: true,
            message: "请选择客户状态",
            trigger: "blur"
          }],
          isAllPay: [{
            required: true,
            message: "请选择付款状态",
            trigger: "blur"
          }],
          level: [{
            required: true,
            message: "请选择客户等级",
            trigger: "blur"
          }],
          productName: [{
            required: true,
            message: "产品包名称不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /**省 市 区*/
      selectLoadOptions(node, resolve) {
        var query = {
          regionLevel: node.level + 1,
          parentId: node.value || null
        }
        loadOptions(query).then(response => {
          resolve(response.data);
        });
      },
      /** 查询销售单列表 */
      getList() {
        this.loading = true;
        listCustomer(this.queryParams).then(response => {
          this.customerList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          custName: null,
          province: null,
          city: null,
          district: null,
          address: null,
          linkName: null,
          linkTel: null,
          productName: null,
          productPrice: null,
          level: null,
          sourceChannel: null,
          signState: null,
          isAllPay: null,
          nextDoUserName: null,
          mark: null,
          deptId: null,
          gmtCreate: null,
          gmtModify: null,
          deleted: null,
          createBy: null,
          updateBy: null,
          provinceAndCityAndDistrict: [],
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加销售单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getCustomer(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改销售单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.submitLoading = true
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateCustomer(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.submitLoading = false
              })
            } else {
              addCustomer(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.submitLoading = false
              })
            }
          } else {
            this.submitLoading = false
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除销售单编号为"' + ids + '"的数据项？').then(function() {
          return delCustomer(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('saledata/customer/export', {
          ...this.queryParams
        }, `customer_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
